home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Shout3Ddemo / Shout3d_runtime / codebase / applets / LightTestPanel.java < prev    next >
Text File  |  2000-09-01  |  5KB  |  159 lines

  1. /**    
  2.     Company:        Eyematic Interfaces
  3.     Project:        Shout3D 2.0 Sample Code
  4.     Class:            LightTestPanel
  5.     Date:            April 26, 1999
  6.     Description:    Class for panel that tests light scoping
  7.     (C) Copyright Eyematic Interfaces, Inc. - 1997-2000 - All rights reserved
  8.  */
  9.  
  10. package applets;
  11.  
  12. import java.applet.*;
  13. import java.awt.*;
  14. import java.awt.image.*;
  15. import java.io.*;
  16. import java.util.Date;
  17. import java.net.URL;
  18.  
  19. import shout3d.*;
  20. import shout3d.core.*;
  21.  
  22. /**
  23.  * LightTestPanel
  24.  * 
  25.  * Shows how a single Light node can be used to light
  26.  * one of several objects, or the entire scene.
  27.  * 
  28.  * All this is done be setting the 'affectedGroups' field of the
  29.  * light.
  30.  * 
  31.  * Clicking or dragging on any of the three colored cubes makes 
  32.  * the light affect that cube.
  33.  * Clicking on the bottom white bar lights everything.
  34.  * 
  35.  * The applet only works if the displayed file contains a light named
  36.  * "MyLight" and  groups named "Box1" "Box2" "Box3" and "BottomBar"
  37.  * 
  38.  * @author Dave Westwood
  39.  * @author Paul Isaacs
  40.  * @author Jim Stewartson
  41.  */
  42.  
  43. public class LightTestPanel extends Shout3DPanel implements DeviceObserver {
  44.     
  45.     // The light that will affect the scene
  46.     DirectionalLight light;
  47.     // Names that will be used for the affectedGroups field
  48.     String box1_names[] = {"Box1"};
  49.     String box2_names[] = {"Box2"};
  50.     String box3_names[] = {"Box3"};
  51.     String root_names[] = {"#Root"};  //Predefined value for lighting the entire scene
  52.     // For determining which object is clicked with the mouse.
  53.     Picker myPicker;
  54.     Node[] pathToPick;
  55.  
  56.     /**
  57.      * Constructor
  58.      */
  59.     public LightTestPanel(Shout3DApplet applet){
  60.         super(applet);
  61.     }
  62.     
  63.     /**
  64.      *
  65.      * This method is automatically called by the parent class Shout3DPanel
  66.      * at the correct time during initialize().
  67.      * 
  68.      * Subclasses should implement this to perform any custom initialization tasks.
  69.      */    
  70.     public void customInitialize() {
  71.         
  72.         // Get the light, have it shine on the first group.
  73.         light = (DirectionalLight)this.getNodeByName("MyLight");
  74.         if (light == null){
  75.             throw new Shout3DException("can not find node name MyLight");
  76.         }
  77.         else {
  78.             light.affectedGroups.setValue(box1_names);
  79.         }
  80.         
  81.         // Allocate the picker
  82.         myPicker = getNewPicker();
  83.         
  84.         //Watch for mouse events to do the picking.
  85.         this.addDeviceObserver(this, "MouseInput", null);
  86.         
  87.         //Call the parent class
  88.         super.customInitialize();
  89.     }
  90.     
  91.     /**
  92.      * Finalize
  93.      */
  94.     protected void finalize(){
  95.         this.removeDeviceObserver(this, "MouseInput");
  96.     }
  97.  
  98.     /** 
  99.      * Called when Mouse input is received.
  100.      * 
  101.      * On mouse DOWN, checks for selection of a node whose 
  102.      * name is one of those we're interested in.  If so,
  103.      * sets the light to affect it.
  104.      * 
  105.      * If the selected group is "BottomBar" then sets the 
  106.      * affectedGroups to be the pre-defined value #Root,
  107.      * which Shout3d will automatically interpret to light 
  108.      * the entire scene.
  109.      *  
  110.      */
  111.     public boolean onDeviceInput(DeviceInput di, Object userData){
  112.         //No need to check type of deviceInput, only registered for Mouse Input.
  113.         MouseInput mi = (MouseInput) di;
  114.         if (mi.which == MouseInput.DOWN || mi.which == MouseInput.DRAG ){
  115.             if (myPicker == null)
  116.                 return false;
  117.         
  118.             // Perform a pick to find what's under the cursor,
  119.             //
  120.             pathToPick = myPicker.pickClosest(mi.x,mi.y);
  121.             if (pathToPick!=null && pathToPick.length > 0){
  122.                 Node lastTransform = null;
  123.                 // Find the last transform on the path.
  124.                 for (int i=pathToPick.length-1;i>-1;i--){
  125.                     if (pathToPick[i] instanceof Transform){
  126.                         lastTransform = pathToPick[i];
  127.                         break;
  128.                     }
  129.                 }
  130.                         
  131.                 //Depending which was picked, set the name in 
  132.                 //the light's affectedGroup field.
  133.                 //The transforms for the objects are named:
  134.                 // "box1" "box2" "box3" "BottomBar"
  135.                 if (lastTransform!=null){
  136.                     String name = lastTransform.getDEFName();
  137.                     if (name!=null){
  138.                         if (name.equals(box1_names[0]))
  139.                             light.affectedGroups.setValue(box1_names);
  140.                         else if (name.equals(box2_names[0]))
  141.                             light.affectedGroups.setValue(box2_names);
  142.                         else if (name.equals(box3_names[0]))
  143.                             light.affectedGroups.setValue(box3_names);
  144.                         else if (name.equals("BottomBar"))
  145.                             light.affectedGroups.setValue(root_names);
  146.                     }
  147.                 }
  148.                 //This input was used
  149.                 return true;
  150.             }
  151.  
  152.         }
  153.         //Did not care about this input
  154.         return false;
  155.     }
  156.     
  157.  
  158. }
  159.